<template>
  <el-dialog
    :title="!ptdataForm.nid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="ptdataForm" :rules="dataRule" ref="ptdataForm" @keyup.enter.native="ptdataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="ptdataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="ptdataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="砍价活动id" prop="bargainId">
      <el-input v-model="ptdataForm.bargainId" placeholder="砍价活动id"></el-input>
    </el-form-item>
    <el-form-item label="最终价格" prop="lastPrice">
      <el-input v-model="ptdataForm.lastPrice" placeholder="最终价格"></el-input>
    </el-form-item>
    <el-form-item label="砍价人数" prop="bargainFlag">
      <el-input v-model="ptdataForm.bargainFlag" placeholder="砍价人数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="ptdataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        ptdataForm: {
          nid: 0,
          userId: '',
          goodsId: '',
          bargainId: '',
          lastPrice: '',
          bargainFlag: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          bargainId: [
            { required: true, message: '砍价活动id不能为空', trigger: 'blur' }
          ],
          lastPrice: [
            { required: true, message: '最终价格不能为空', trigger: 'blur' }
          ],
          bargainFlag: [
            { required: true, message: '砍价人数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.ptdataForm.nid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['ptdataForm'].resetFields()
          if (this.ptdataForm.nid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/notes/info/${this.ptdataForm.nid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.ptdataForm.userId = data.notes.userId
                this.ptdataForm.goodsId = data.notes.goodsId
                this.ptdataForm.bargainId = data.notes.bargainId
                this.ptdataForm.lastPrice = data.notes.lastPrice
                this.ptdataForm.bargainFlag = data.notes.bargainFlag
              }
            })
          }
        })
      },
      // 表单提交
      ptdataFormSubmit () {
        this.$refs['ptdataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/notes/${!this.ptdataForm.nid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'nid': this.ptdataForm.nid || undefined,
                'userId': this.ptdataForm.userId,
                'goodsId': this.ptdataForm.goodsId,
                'bargainId': this.ptdataForm.bargainId,
                'lastPrice': this.ptdataForm.lastPrice,
                'bargainFlag': this.ptdataForm.bargainFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
